package com.mopa.renren;
import java.net.URLDecoder;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

public class RenShareActivity extends Activity {
	private AuthReceiver receiver;
	public String mAccessToken, mOpenId;
	private RenRenLayout qLayout;
	public String mAppid=RenRenItem.APPID;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        qLayout=new RenRenLayout(this);
        setContentView(qLayout);
        registerIntentReceivers();
    }
    public class AuthReceiver extends BroadcastReceiver {
    	@Override
    	public void onReceive(Context context, Intent intent) {
 			String result=intent.getStringExtra("result");
 			String callback=intent.getStringExtra("callback");
 			result=URLDecoder.decode(result);
 			if(callback.equals(RenRenItem.CALLBACK))
 			{
 				getRenAtoken(result);
 			}

    	}

    }
    public void getRenAtoken(String result)
    {
    	
    	Log.e("debug", "result:"+result);
    	Bundle bundle=WUtils.parseResult(result.toString(), RenRenItem.CALLBACK);
    	String access_token=bundle.getString("access_token");
    	String error_ret=bundle.getString("error");
    	String error_des=bundle.getString("error_description");
    	String expires_in=bundle.getString("expires_in");
    	Log.e("debug", "access_token"+access_token+"  error_ret"+error_ret+"  error_des"+error_des+"  expires_in"+expires_in);
    	if (access_token != null)
    	{
    		mAccessToken = access_token;
    	}

    }
    
    public static final int REQUEST_PICK_PICTURE = 1001;
	public void showMessage(String title, String msg) {
		Dialog dialog = new Dialog(RenShareActivity.this);
		ScrollView rootView = new ScrollView(RenShareActivity.this);
		TextView view = new TextView(RenShareActivity.this);
		view.setText(msg);
		rootView.addView(view);
		dialog.setContentView(rootView);
		dialog.setTitle(title);
		dialog.show();
	}
	
	public boolean satisfyConditions() {
		return 	mAccessToken != null && 
		!mAccessToken.equals("");
	}
	
	public static final int PROGRESS = 0;
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case PROGRESS:
			dialog = new ProgressDialog(this);
			((ProgressDialog)dialog).setMessage("Loading..");
			break;
		}
		return dialog;
	}
	
	private void registerIntentReceivers() {
		receiver =  new AuthReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WebViewActivity.AUTH_BROADCAST);
		registerReceiver(receiver, filter);
	}
	
	private void unregisterIntentReceivers() {
		unregisterReceiver(receiver);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_PICK_PICTURE: {
			if (data != null) {
				Uri bitmapUri = data.getData();
				if (bitmapUri != null) {
					UploadPicClickListener.uploadPic(this, bitmapUri);
				}
			}
			break;
		}
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
    	if (receiver != null) {
        	unregisterIntentReceivers();
		}
	}

}